﻿/// <reference path="vswd-ext_2.2.js" />

//load trackname

// Parking report

function ParkWindow() {
	var trackerstore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: 'HandlerSearchTrackerName'
		}),
		reader: new Ext.data.JsonReader({
			fields: [{ name: 'trackname' }, { name: 'snid'}],
			root: "data"
		})
	});

	trackerstore.load();

	var trackNameCombo = new Ext.ux.form.CheckboxCombo({
		name: 'tracker',
		store: trackerstore,
		mode: 'local',
		displayField: 'trackname',
		width: 230,
		valueField: 'snid',
		hiddenName: 'tracker',
		enableKeyEvents: true,
		editable: true
	});
	
	trackNameCombo.on('keyup', function() {
		trackerstore.filter('trackname', trackNameCombo.getRawValue(), true, false);
		trackNameCombo.bindStore(trackerstore);
	});

	trackNameCombo.on('blur', function() {
		trackerstore.clearFilter();
		trackNameCombo.bindStore(trackerstore);
	});
	
	var pagesize = 15
	var day = new Date();
	var today = day.getFullYear() + '-' + padLeft((day.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(day.getDate().toString(), "0", 2);
	var yyday = addDays(day, -1);
	var yestoday = yyday.getFullYear() + '-' + padLeft((yyday.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(yyday.getDate().toString(), "0", 2);
	
	var fields = ["sn_imei_id", "StartTime", "EndTime", 'Period','Longitude','Latitude', 'ParkAddress', 'Name'];
	var fromDateEditor = new Ext.form.DateField({
		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		format: 'Y-m-d',
		value: today
	});

	var fromTimeEditor = new Ext.form.TimeField({
		value:"00:00",
		format: 'H:i',
		width: 70
	});

	var lengthTimeEditor = new Ext.form.TimeField({
		minValue:"00:03",
		value:"00:03",
		format: 'H:i',
		width: 70,
		increment: 1
	});
	
	var toDateEditor = new Ext.form.DateField({
		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		format: 'Y-m-d',
		value: today
	});

	var toTimeEditor = new Ext.form.TimeField({
		value:'23:59',
		format: 'H:i',
		width: 70
	});
	
	var parkingstore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: "HandlerGetAllPartInfo",
			method: "POST"
		}),
		reader: new Ext.data.JsonReader({
			fields: fields,
			root: "data",
			totalProperty: "totalCount"
		})
	});

	var addressCb = new Ext.form.Checkbox({ checked: false });

	var tbar = new Ext.Toolbar({
		items: [
		_Alarm_TrackerName + ':',
		trackNameCombo,
		_Alarm_ParkingTime + ':',
		lengthTimeEditor,
		 {
			 xtype: 'tbseparator', width: 5
		 },
		_Alarm_FromTime + ':',
		fromDateEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		fromTimeEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		_Alarm_ToTime + ':',
		toDateEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		toTimeEditor, {
			xtype: 'tbseparator', width: 5
		},
		_TrackerMain_ARP,
		addressCb,
		{ tooltip: { text: _TrackerMain_Search, autoHide: true },
			icon: 'Content/images/search16.gif',
			handler: function () {

				parkingstore.load({ 
					params: {
						start: 0,
						limit: pagesize,
						snid: trackNameCombo.getValue(),
						fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
						totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
						Length: lengthTimeEditor.getEl().dom.value
					}
				})
			}
		},
		{
			tooltip: { text: _TrackerMain_ExportToExcel, autoHide: true },
			icon: 'Content/images/excel16.gif',
			handler: function () {
				if (!Ext.fly('parkingexcel')) {
					var frm = document.createElement('form');
					frm.id = 'parkingexcel';
					frm.name = id;
					frm.style.display = 'none';
					document.body.appendChild(frm);
				}
				var requestaddress = false;
				requestaddress = addressCb.getValue();
				if (typeof (pcsaddress) != 'undefined') {
					requestaddress = pcsaddress && addressCb.getValue();
				}
				Ext.Ajax.request({
					url: 'HandlerExcel',
					form: Ext.fly('parkingexcel'),
					method: 'POST',
					params: {
						exceltype: 'parkingall',
						start: 0,
						limit: 60000,
						snid: trackNameCombo.getValue(),
						fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
						totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
						Length: lengthTimeEditor.getEl().dom.value,
						requestaddress: requestaddress ? "1" : "0"
					},
					isUpload: true
				});
			}
		}
		/*
		, {
			tooltip: { text: _TrackerMain_ExportToKML, autoHide: true },
			icon: 'Content/images/kml.gif',
			handler: function () {
				if (!Ext.fly('parkingkml')) {
					var frm = document.createElement('form');
					frm.id = 'parkingkml';
					frm.name = id;
					frm.style.display = 'none';
					document.body.appendChild(frm);
				}
				Ext.Ajax.request({
					url: 'servicesandhandler/HandlerParkingKML.ashx',
					form: Ext.fly('parkingkml'),
					method: 'POST',
					params: {
						exceltype: 'parking',
						start: 0,
						limit: 60000,
						trackName: trackNameCombo.getRawValue(),
						snid: trackNameCombo.getValue(),
						fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
						totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
						Length: lengthTimeEditor.getEl().dom.value
					},
					isUpload: true
				});

			}
		}
		*/
		]

	});

	var grid = new Ext.grid.GridPanel({
		store: parkingstore,
		loadMask: true,
		title: _Alarm_ParkingGrid,
		columns: [

			{ header: _Alarm_Header_Name, width: 100, sortable: true, menuDisabled: true, dataIndex: 'Name' },
			{ header: _Alarm_Header_StartTime, width: 180, sortable: true, menuDisabled: true, dataIndex: 'StartTime' },
			{ header: _Alarm_Header_EndTime, width: 180, sortable: true, menuDisabled: true, dataIndex: 'EndTime' },
			{ header: _Alarm_Header_Latitude, width: 75, sortable: true, menuDisabled: true, dataIndex: 'Latitude' },
			{ header: _Alarm_Header_Longitude, width: 75, sortable: true, menuDisabled: true, dataIndex: 'Longitude' },
			{ header: _Alarm_Spot, width: 200, sortable: true, menuDisabled: true, dataIndex: 'ParkAddress' },
			{ header: _Alarm_Period, width: 120, sortable: true, menuDisabled: true, dataIndex: 'Period' }


		],
		width: 780,
		height: 480
	});
	var parkingInterval;
	parkingstore.on('load', function(store) {
		if (addressCb.getValue() == false)
			return;

		if (!Ext.isEmpty(parkingInterval))
			window.clearInterval(parkingInterval);
		var count = parkingstore.getCount(), recycleTimes = 0;
		if (count > 0) {
			var i = 0;
			parkingInterval = window.setInterval(function() {
				if (recycleTimes >= 10) {
					i++;
					recycleTimes = 0;
				}
				if (i == count) {
					window.clearInterval(parkingInterval);
					return;
				}
				else {
					var record = store.getAt(i);
					imap.geoCoder( record.data.Latitude,record.data.Longitude,
								function(address, index) {
									if (address == "") {
										recycleTimes++;
										return;
									}
									record.set("ParkAddress", address);
									i += index;
								});
				}
			}, 1000);
		}
		else {
			Ext.Msg.alert(_Alarm_NoData, _Alarm_NoData);
		}
	});
	//if (isOpenExtentFn) {
		grid.addListener("rowdblclick", function(grid, index) {
			var record = grid.getStore().getAt(index);
			/*
			var win = createCommonMapWindow( "", 600, 450, true, false, false, false);
			win.show(); // 显示出来
			nextwindowx += 10;
			nextwindowy += 0;
			var rmap = createMap(win.getMap(), true);
			rmap.drawParkingPoint(record);
			*/
			var longitude = parseFloat(record.get("Longitude"));
			var latitude = parseFloat(record.get("Latitude"));
			var newMap = Sample.ExtGMap.initpos(latitude,longitude);

			var gmapwindow = new Ext.Window({
				title: '(' + record.get('devicename') + ')',
				closable: true,
				width: 500,
				height: 450,
				minimizable: false,
				items: [newMap]
			});
			gmapwindow.show();

		});
	//}
	grid.addListener('render', function(grid) {
		var store = grid.getStore();  // Capture the Store.	
		var view = grid.getView();	// Capture the GridView.
		grid.tip = new Ext.ToolTip({
			target: view.mainBody,	// The overall target element.
			//delegate: '.x-grid3-row', // Each grid row causes its own seperate show and hide.	
			delegate: '.x-grid3-cell', // Each grid cell causes its own seperate show and hide.	
			trackMouse: true,		 // Moving within the row should not hide the tip.	
			renderTo: document.body,  // Render immediately so that tip.body can be referenced prior to the first show.	
			listeners: {			  // Change content dynamically depending on which element triggered the show.	
				beforeshow: function updateTipBody(tip) {
					var rowIndex = view.findRowIndex(tip.triggerElement);
					var cellIndex = view.findCellIndex(tip.triggerElement);
					if (cellIndex == 4) {
						var cell = view.getCell(rowIndex, cellIndex);
						if (cell.innerText.length > 0) {
							tip.body.dom.innerHTML = cell.innerText;
							return true;
						}
						else
							return false;
					}
					else {
						return false;
					}
				}
			}
		});
	});
	var parkwindow = new Ext.Window({
		title: _Alarm_ParkInfo,
		layout: 'fit',
		height: 490,
		width: 1150,
		iconCls: 'report-parking',
		items: grid,
		tbar: tbar,
		closeAction: 'close',
		bbar: new Ext.PagingToolbar({
			pageSize: pagesize,
			store: parkingstore,
			beforePageText: _Alarm_Page,
			refreshTex: _Alarm_Refresh,
			displayMsg: _Alarm_Displaying,
			emptyMsg: _Alarm_Alarm_emptyMsg,
			displayInfo: true
		}),
		minimizable: true,
		listeners: {
			'minimize': function() {
				addTaskButtonForWindow(parkwindow);
			}
		}
	});
	parkingstore.on('beforeload', function(store) {
		parkingstore.baseParams = { start: 'start', limit: 'limit',
			snid: trackNameCombo.getValue(),
			fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
			totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value, Length: lengthTimeEditor.getEl().dom.value
		}
	})
	parkwindow.addListener('beforeclose', function() {
		if (!Ext.isEmpty(parkingInterval)) {
			window.clearInterval(parkingInterval);
		}
	});
	parkwindow.show();
};
// speedChart
function speedChart() {
	var trackerstore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: 'HandlerSearchTrackerName'
		}),
		reader: new Ext.data.JsonReader({
			fields: [{ name: 'trackname' }, { name: 'snid'}],
			root: "data"
		})
	});

	trackerstore.load()
	var trackNameCombo = new Ext.form.ComboBox({
		name: 'tracker1',
		store: trackerstore,
		mode: 'local',
		displayField: 'trackname',
		width: 230,
		valueField: 'snid',
		hiddenName: 'tracker1',
		disableKeyFilter :true
	});


	var day = new Date();
	var today = day.getFullYear() + '-' + padLeft((day.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(day.getDate().toString(), "0", 2);
	var yyday = addDays(day, -1);
	var yestoday = yyday.getFullYear() + '-' + padLeft((yyday.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(yyday.getDate().toString(), "0", 2);
	
	var fields = ["sn_imei_id", "StartTime", "EndTime", 'Period'];
	var fromDateEditor = new Ext.form.DateField({
		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		format: 'Y-m-d',
		value: today
	});


	var fromTimeEditor = new Ext.form.TimeField({
		value:"00:00",
		format: 'H:i',
		width: 70
	});

	var toDateEditor = new Ext.form.DateField({
		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		format: 'Y-m-d',
		value: today
	});

	var toTimeEditor = new Ext.form.TimeField({
		value:'23:59',
		format: 'H:i',
		width: 70
	});
	var store = new Ext.data.JsonStore({

		url: 'HandlerGetSpeedInfo',
		root: 'data',
		fields: ['SpeedSegment', 'Total', 'Address', 'CellPhone', 'TelePhone', 'Credit', 'DaysCredit', 'Limit', 'ExpiredTime']

	});
 
	
	var tbar = new Ext.Toolbar({
		items: [_Alarm_TrackerName + ':',
		trackNameCombo,
		 {
			 xtype: 'tbseparator', width: 5
		 },
		_Alarm_FromTime + ':',
		fromDateEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		fromTimeEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		_Alarm_ToTime + ':',
		toDateEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		toTimeEditor, { tooltip: { text: _TrackerMain_Search, autoHide: true }, 
			icon: 'Content/images/search16.gif', handler: function() {
			Ext.MessageBox.show({
				msg: _CustomerConfigResource_Loading,
				progressText: _CustomerConfigResource_Loading,
				width: 300,
				wait: true,
				waitConfig: { interval: 2000 }
			});
			store.load({ params: {
				snid: trackNameCombo.getValue(),
				fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
				totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value
			}, callback: function() {
				Ext.MessageBox.hide();
				var count = store.getCount();
				if (count <= 0) {
					Ext.Msg.alert(_Alarm_NoData, _Alarm_NoData);
				}
			}

			})
		}}
		]

	});
	var speedPanel = new Ext.Panel({
		width: 780,
		height: 400,
		title: _TrackerMain_Chart,
		items: {
			store: store,
			xtype: 'piechart',
			dataField: 'Total',
			categoryField: 'SpeedSegment',
			extraStyle:
			{
				legend:
				{
					display: 'bottom',
					padding: 5,
					font:
					{
						family: 'Tahoma',
						size: 13
					}
				}
			}
		}
	});
	var win = new Ext.Window({
		title: _Alarm_SpeedChart,
		layout: 'fit',
		height: 465,
		width: 800,
		tbar: tbar,
		iconCls: 'pie-speed',
		closeAction: 'hide',
		buttonAlign: "center",
		items: [speedPanel],
		minimizable: true,
		listeners: {
			'minimize': function() {
				addTaskButtonForWindow(win);
			}
		}
	});
	store.on('beforeload', function(store) {
		store.baseParams = { snid: trackNameCombo.getValue(), fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
			totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value
		}
	})
	win.show();

}



// distanceWindow
function distanceWindow() {
	var trackerstore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: 'HandlerSearchTrackerName'
		}),
		reader: new Ext.data.JsonReader({
			fields: [{ name: 'trackname' }, { name: 'snid'}],
			root: "data"
		})
	});

	trackerstore.load()

	var trackNameCombo = new Ext.ux.form.CheckboxCombo({
		name: 'tracker2',
		store: trackerstore,
		mode: 'local',
		displayField: 'trackname',
		width: 230,
		valueField: 'snid',
		hiddenName: 'tracker2',
		enableKeyEvents: true,
		editable: true
	});

	trackNameCombo.on('keyup', function() {
		trackerstore.filter('trackname', trackNameCombo.getRawValue(), true, false);
		trackNameCombo.bindStore(trackerstore);
	});

	trackNameCombo.on('blur', function() {
		trackerstore.clearFilter();
		trackNameCombo.bindStore(trackerstore);
	});
	
	var pagesize = 15
	var day = new Date();
	var today = day.getFullYear() + '-' + padLeft((day.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(day.getDate().toString(), "0", 2);
	var yyday = addDays(day, -1);
	var yestoday = yyday.getFullYear() + '-' + padLeft((yyday.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(yyday.getDate().toString(), "0", 2);
	
	var fields = ["sn_imei_id", "DeviceName", "StartLatLng", "EndLatLng", 'Period', 'AverageSpeed', 'Distance', 'StartTime', 'EndTime'];
	var fromDateEditor = new Ext.form.DateField({
		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		format: 'Y-m-d',
		value: today
	});


	var fromTimeEditor = new Ext.form.TimeField({
		value:"00:00",
		format: 'H:i',
		width: 70
	});

	var toDateEditor = new Ext.form.DateField({
		renderer: Ext.util.Format.dateRenderer('Y-m-d'),
		format: 'Y-m-d',
		value: today
	});

	var toTimeEditor = new Ext.form.TimeField({
		value:'23:59',
		format: 'H:i',
		width: 70
	});

	var distancestore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: "HandlerGetDistanceInfo",
			method: "POST"
		}),
		reader: new Ext.data.JsonReader({
			fields: fields,
			root: "data",
			totalProperty: "totalCount"
		})
	});


	/*var distanceUnitChangeComb = new Ext.form.ComboBox({
		name: 'distanceUnit',
		store: new Ext.data.ArrayStore({
			autoDestroy: true,
			idIndex: 0,
			fields: ['id', 'text'],
			data: [[1, _Distance_Mile], [2, _Distance_Kilometer]]
		}),
		mode: 'local',
		displayField: 'text',
		width: 80,
		value: 2,
		triggerAction: 'all',
		valueField: 'id',
		editable: false,
		listeners: {
			select: function() {
				//转换里程头
				grid.getColumnModel().setColumnHeader(6, _Distance_Distance + "(" + this.getRawValue() + ")");
			}
		}
	});*/
	var addressCb = new Ext.form.Checkbox({ checked: false });
	var numStartSpeed = new Ext.form.NumberField({
		maxLength: 3,
		width: 20,
		allowBlank: false,
		value:5
	});
	
	var numParkingMinutes =  new Ext.form.NumberField({
		maxLength: 3,
		width: 20,
		allowBlank: false,
		value:5
	});

	var numDistance = new Ext.form.NumberField({
		maxLength:5,
		width:30,
		allowBlank: false,
		value:1000
	});
	
	var tbar = new Ext.Toolbar({
		items: [
		_Alarm_TrackerName + ':',

		trackNameCombo,
		 {
			 xtype: 'tbseparator', width: 5
		 },
		_Alarm_FromTime + ':',
		fromDateEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		fromTimeEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		_Alarm_ToTime + ':',
		toDateEditor,
		{
			xtype: 'tbseparator', width: 5
		},
		toTimeEditor, {
			xtype: 'tbseparator', width: 5
		},
		_TrackerMain_StartSpeed,
		numStartSpeed,
		_TrackerMain_ParkingMinutes,
		numParkingMinutes,
		// _TrackerMain_Distance + "(" + _TrackerMain_Meter +")>",
		// numDistance ,
		_TrackerMain_ARP,
		addressCb, '-', //_Distance_Unit+":", distanceUnitChangeComb, '-',
		{ tooltip: { text: _TrackerMain_Search, autoHide: true }, icon: 'Content/images/search16.gif',
			handler: function() {

				distancestore.load({ params: {
					start: 0,
					limit: pagesize,
					snid: trackNameCombo.getValue(),
					fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
					totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
					startspeed: numStartSpeed.getValue(),
					stopminutes: numParkingMinutes.getValue(),
					distance:500 //numDistance.getValue()
				}
				})

			}
		}, {
			tooltip: { text: _TrackerMain_ExportToExcel, autoHide: true },
			icon: 'Content/images/excel16.gif',
			handler: function() {
				if (!Ext.fly('distanceexcel')) {
					var frm = document.createElement('form');
					frm.id = 'distanceexcel';
					frm.name = id;
					frm.style.display = 'none';
					document.body.appendChild(frm);
				}
				var requestaddress = false;
				requestaddress = addressCb.getValue();
				if (typeof (pcsaddress) != 'undefined') {
					requestaddress = pcsaddress && addressCb.getValue();
				}
				Ext.Ajax.request({
					url: 'HandlerExcel',
					form: Ext.fly('distanceexcel'),
					method: 'POST',
					params: {
						exceltype: 'distanceall',
						start: 0,
						limit: 60000,
						name: trackNameCombo.lastSelectionText,
						snid: trackNameCombo.getValue(),
						fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
						totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
						startspeed: numStartSpeed.getValue(),
						stopminutes: numParkingMinutes.getValue(),
						requestaddress: requestaddress ? "1" : "0",
						distance: 500//, //numDistance.getValue()
						//distanceunit:distanceUnitChangeComb.getValue()
					},
					isUpload: true
				});
			}
		}
		]

	});

	var distancestr = _Distance_Distance + "(" + _Distance_Kilometer + ")";
	if (showmiles)
		distancestr = _Distance_Distance + "(" + _TrackerMain_Miles + ")";
	var grid = new Ext.grid.GridPanel({
		store: distancestore,
		loadMask: true,
		columns: [
			{ header: _Alarm_Header_Name, width: 100, sortable: true, menuDisabled: true, dataIndex: 'DeviceName' },
			{ header: _Alarm_Header_StartTime, width: 150, sortable: true, menuDisabled: true, dataIndex: 'StartTime' },
			{ header: _Alarm_Header_EndTime, width: 150, sortable: true, menuDisabled: true, dataIndex: 'EndTime' },
			{ header: _Alarm_Period, width: 120, sortable: true, menuDisabled: true, dataIndex: 'Period' },
			{ header: _Alarm_StartLatLng, width: 200, sortable: true, menuDisabled: true, dataIndex: 'StartLatLng' },
			{ header: _Alarm_EndLatLng, width: 200, sortable: true, menuDisabled: true, dataIndex: 'EndLatLng' },
		//			{header: _Alarm_ASpeed, width: 100, sortable: true, menuDisabled: true,dataIndex: 'AverageSpeed'},
			{header: distancestr, width: 100, sortable: true, menuDisabled: true, dataIndex: 'Distance', renderer: function(value) {
				if (showmiles) {
					if (value.indexOf(':') >= 0) {
						//return value.split(':')[0] + String((Math.floor(parseFloat(value.split(':')[1]) / 1.6093 * 100)) / 100);
						return value.split(':')[0] +":" + kilos2miles(value.split(':')[1]);
					}
					else {
						return kilos2miles(value);
						//return String((Math.floor(parseFloat(value) / 1.6093 * 100)) / 100);
					}
				}
				else {
					return value;
				}
			}
		}
		],
		width: 970,
		height: 480
	});

	var trackpointsfields = ["gpstime", "latitude", "longitude", "speed", "direction", "otherinfo", { name: "stopped", type: "bool" }, "stoppedtime"];

	var trackpointstore = new Ext.data.Store({
		reader: new Ext.data.JsonReader({
			fields: trackpointsfields,
			root: "data",
			stoptime: "stoptime", //本次搜索停止时间
			end: "end"
		}),
		proxy: new Ext.data.HttpProxy({
			url: 'servicesandhandler/HandlerGetTrackPoints.ashx',
			timeout: 240000
		})

	});


	grid.addListener('rowdblclick', function(grid, rowindex, e) {
		var trackName = distancestore.getAt(rowindex).get("DeviceName");
		var firsttime = distancestore.getAt(rowindex).get("StartTime");
		var lasttime = distancestore.getAt(rowindex).get("EndTime");

		trackpointstore.reload({
			params: {
				trackName: trackName,
				fromtime: firsttime,
				totime: lasttime

			},

			callback: function() {
				Ext.MessageBox.hide();
				if (trackpointstore.getCount() > 0) {
					var rmap;
					var butStartStop = new Ext.Button({
						tooltip: { text: _AnimateControl_Stop, autoHide: true },
						icon: 'Content/images/player_pause.png',
						handler: function() {
							if (butStartStop.tooltip.text == _AnimateControl_Stop) {
								rmap.stopAnimate();
								butStartStop.setTooltip({ text: _AnimateControl_Continue, autoHide: true });
								butStartStop.setIcon('Content/images/player_play.png');
							}
							else {
								rmap.startAnimate();
								butStartStop.setTooltip({ text: _AnimateControl_Stop, autoHide: true });
								butStartStop.setIcon('Content/images/player_stop.png');
							}

						}
					});
					var tbarmap = new Ext.Toolbar({
						items: [{
							tooltip: { text: _AnimateControl_ToStart, autoHide: true },
							icon: 'Content/images/player_start.png',
							handler: function() {
								rmap.toStartPoint();
							}
						}, {
							tooltip: { text: _AnimateControl_SlowDown, autoHide: true },
							icon: 'Content/images/player_rew.png',
							handler: function() {
								rmap.slowDownAnimate();
							}
						}, butStartStop
								, {
									tooltip: { text: _AnimateControl_SpeedUp, autoHide: true },
									icon: 'Content/images/player_fwd.png',
									handler: function() {
										rmap.speedUpAnimate();
									}
								}, {
									tooltip: { text: _AnimateControl_ToEnd, autoHide: true },
									icon: 'Content/images/player_end.png',
									handler: function() {
										rmap.toEndPoint();
									}
								}

										]

					});

					var win = createHistoryMapWindow(_TrackerMain_HistoryLine + '(' + trackName + ')', tbarmap);

					//win.tbar = tbarmap;
					win.show(); // 显示出来
					rmap = createMap(win.getMap(), true, null, gmapurl);
					
					nextwindowx += 20;
					nextwindowy += 20;

					rmap.setCenterbyLatLng(0, 0);
					rmap.drawAnimateHistoryLine(trackpointstore, _TrackerMain_GPSTime, _TrackerMain_Speed);


				}
				else {
					Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
					Ext.Msg.show({
						title: _TrackerMain_Title,
						msg: _Message_NoDataInSelection,
						buttons: Ext.Msg.OK,
						animEl: 'elId',
						icon: Ext.MessageBox.WARNING
					});
				}
			}
		})
	});

	var distancewindow = new Ext.Window({
		title: _Alarm_DistanceInfo,
		layout: 'fit',
		height: 490,
		width: 1200,
		items: grid,
		iconCls: 'report-driving',
		tbar: tbar,
		closeAction: 'close',
		bbar: new Ext.PagingToolbar({
			pageSize: pagesize,
			store: distancestore,
			beforePageText: _Alarm_Page,
			refreshTex: _Alarm_Refresh,
			displayMsg: _Alarm_Displaying,
			emptyMsg: _Alarm_Alarm_emptyMsg,
			displayInfo: true
		}),
		minimizable: true,
		listeners: {
			'minimize': function() {
				addTaskButtonForWindow(distancewindow);
			}
		}
	});
	distancestore.on('beforeload', function(store) {
		distancestore.baseParams = { start: 'start', limit: 'limit', snid: trackNameCombo.getValue(), fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
			totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value, startspeed: numStartSpeed.getValue(),
			stopminutes: numParkingMinutes.getValue(),
			distance: 500 //numDistance.getValue()
		}
	});
	var readAddressInterval1, readAddressInterval2;
	distancestore.on('load', function(store) {
		if (addressCb.getValue() == false)
			return;

		if (!Ext.isEmpty(readAddressInterval1))
			window.clearInterval(readAddressInterval1);

		if (!Ext.isEmpty(readAddressInterval2))
			window.clearInterval(readAddressInterval2);

		var count = store.getCount(), recycleTimes = 0;


		if (count > 1) {
			var i = 0;
			readAddressInterval1 = window.setInterval(function() {

				if (recycleTimes >= 10) {
					i++;
					recycleTimes = 0;
				}
				if (i == count - 1) {
					window.clearInterval(readAddressInterval1);
					i = 0;
					readAddressInterval2 = window.setInterval(function() {
						if (i == count - 1) {
							window.clearInterval(readAddressInterval2);

							i = 0;
							return;
						}
						var record = store.getAt(i);

						imap.geoCoder(String(record.data.EndLatLng).split(',')[0], String(record.data.EndLatLng).split(',')[1],
											function(address, index) {
												if (address == "") {
													recycleTimes++;
													return;
												}
												else
													record.set("EndLatLng", address);
												i += index;
											});

					}, 1500);
					return;
				}
				var record = store.getAt(i);
				imap.geoCoder(String(record.data.StartLatLng).split(',')[0], String(record.data.StartLatLng).split(',')[1],
								function(address, index) {
									if (address == "")
										return;
									record.set("StartLatLng", address);
									i += index;
								});
			}, 1500);


		}
		else {
			Ext.Msg.alert(_Alarm_NoData, _Alarm_NoData);
		}
	});

	grid.addListener('render', function(grid) {
		var store = grid.getStore();  // Capture the Store.	
		var view = grid.getView();	// Capture the GridView.
		grid.tip = new Ext.ToolTip({
			target: view.mainBody,	// The overall target element.
			//delegate: '.x-grid3-row', // Each grid row causes its own seperate show and hide.	
			delegate: '.x-grid3-cell', // Each grid cell causes its own seperate show and hide.	
			trackMouse: true,		 // Moving within the row should not hide the tip.	
			renderTo: document.body,  // Render immediately so that tip.body can be referenced prior to the first show.	
			listeners: {			  // Change content dynamically depending on which element triggered the show.	
				beforeshow: function updateTipBody(tip) {
					var rowIndex = view.findRowIndex(tip.triggerElement);
					var cellIndex = view.findCellIndex(tip.triggerElement);
					if (cellIndex == 4 || cellIndex == 5) {
						var cell = view.getCell(rowIndex, cellIndex);
						if (cell.innerText.length > 0) {
							tip.body.dom.innerHTML = cell.innerText;
							return true;
						}
						else
							return false;
					}
					else {
						return false;
					}
				}
			}
		});
	});
	distancewindow.addListener('beforeclose', function() {
		if (!Ext.isEmpty(readAddressInterval1)) {
			window.clearInterval(readAddressInterval1);
		}
		if (!Ext.isEmpty(readAddressInterval2)) {
			window.clearInterval(readAddressInterval2);
		}
	});
	distancewindow.show();
};
